using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

/// <summary>
/// Summary description for Log
/// </summary>
namespace SDT.Libraries.Utilities
{
    public class LogException
    {
        public static string FilePath { 
            
            get 
            {
                //return @"C:\";          //Log Default
                return @"D:\Log_Lay_Tin_Tu_Dong";    // Log FTC.BDS  
            }
        
        }   
        //trungdt
        private object syncObj = new object();
        public  void Write(string logMessage)
        {
            lock (syncObj)
            {
                try
                {
                    WriteLog(String.Format("{0}\\log_{1:dd.MM.yyyy}.txt", FilePath, DateTime.Now), logMessage);
                }
                catch (Exception ext)
                {
                    WriteLog(String.Format("{0}\\log.txt", FilePath), ext.Message);
                }
            }
        }
        /// <summary>
        /// lock tag
        /// </summary>
        private object syncObjTag = new object();
        public void WriteTag(string logMessage)
        {
            lock (syncObjTag)
            {
                try
                {
                    WriteLog(String.Format("{0}\\logtag_{1:dd.MM.yyyy}.txt", FilePath, DateTime.Now), logMessage);
                }
                catch (Exception ext)
                {
                    WriteLog(String.Format("{0}\\logtag.txt", FilePath), ext.Message);
                }
            }
        }
        /// <summary>
        /// lock savefile
        /// </summary>
        private object syncObjSaveFile = new object();
        public void WriteSaveFile(string logMessage)
        {
            lock (syncObjSaveFile)
            {
                try
                {
                    WriteLog(String.Format("{0}\\logsavefile_{1:dd.MM.yyyy}.txt", FilePath, DateTime.Now), logMessage);
                }
                catch (Exception ext)
                {
                    WriteLog(String.Format("{0}\\logsavefile.txt", FilePath), ext.Message);
                }
            }
        }
        //public static void Write(string logMessage)
        //{
        //    try
        //    {
        //        WriteLog(String.Format("{0}\\log_{1:dd.MM.yyyy}.txt", FilePath, DateTime.Now), logMessage);
        //    }
        //    catch(Exception ext)
        //    {
        //        WriteLog(String.Format("{0}\\log.txt", FilePath), ext.Message);
        //    }
        //}

        public static void WriteLog(string logMessage)
        {
            FileInfo oFileInfo = new FileInfo(FilePath);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.WriteLine("<Log Entry> : {0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.WriteLine("<Message>   : " + logMessage);
                    w.WriteLine("--------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        }

        public void Flush() 
        {
            FileInfo oFileInfo = new FileInfo(FilePath);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        
        
        }
        public void AddEntry(string logMessage, int id)
        {
            FileInfo oFileInfo = new FileInfo(FilePath);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.WriteLine("{0} {1} {2}", id.ToString(), DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.WriteLine(logMessage);
                    w.WriteLine("--------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        }

        public void AddEntry(string logMessage)
        {
            FileInfo oFileInfo = new FileInfo(FilePath);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.WriteLine(logMessage);
                    w.WriteLine("--------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        }
        public  void Write(string path, string logMessage)
        {
            try
            {
                WriteLog(String.Format("{0}\\log_{1:dd.MM.yyyy}.txt", path, DateTime.Now), logMessage);
            }
            catch (Exception ext)
            {
                WriteLog(String.Format("{0}\\log.txt", path), ext.Message);
            }
        }

        private static void WriteLog(string path, string logMessage)
        {
            FileInfo oFileInfo = new FileInfo(path);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.WriteLine("<Log Entry> : {0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.WriteLine("<Message>   : " + logMessage);
                    w.WriteLine("--------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        }

     
      
    }
    
}